<template>
  <div class="imgshop">
    <div class="contentBox">
      <div class="imgBox">
        <div class="top1">
          <div class="top1Title">图片素材库</div>
          <div class="top1Bottom">
            <div class="bottomLeft">
              {{ radioSelect != "" ? `选中1个` : "选中0个" }}
            </div>
            <div class="bottomRight">
              <el-button type="warning" @click="cancel">取消</el-button>
              <el-button type="warning" @click="save">保存</el-button>
            </div>
          </div>
        </div>
        <div class="table">
          <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
          >
            <el-table-column label="选择" width="55">
              <template slot-scope="scope">
                <el-radio
                  v-model="radio"
                  :label="scope.$index"
                  @click.native="getRowInfo(scope.$index)"
                >
                </el-radio>
              </template>
            </el-table-column>
            <el-table-column label="图片" width="120">
              <template slot-scope="scope">
                <img :src="scope.row.imgUrl" class="img" />
              </template>
            </el-table-column>
            <el-table-column
              prop="imgName"
              label="图片名称"
              show-overflow-tooltip
            >
            </el-table-column>
          </el-table>
        </div>
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          :total="total"
          class="pagination"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      radio: "",
      radioSelect: "",
      imgGroupId: "",
      tableData: [],
      currentPage: 1,
      pageSize: 20,
      total: 0
    };
  },
  created() {
    // 获取表格数据
    this.getTableData();
  },
  activated() {
    // 获取表格数据
    this.getTableData();
  },
  methods: {
    getTableData() {
      this.$http({
        url: "/cmsadmin/index/materialLibrary/fetchMaterialLibraryList",
        method: "get",
        params: {
          page: this.currentPage,
          size: this.pageSize,
          imgGroupId: this.imgGroupId
        }
      }).then(res => {
        const { list, pageSize, pageNum, total } = res;
        if (list.length >= 0) {
          this.tableData = list;
        }
        this.pageSize = pageSize;
        this.currentPage = pageNum;
        this.total = total;
      });
    },
    handleCurrentChange(val) {
      // 改变页码
      this.currentPage = val;
      this.getTableData();
      // 翻页清除用户选择的内容
      this.radio = "";
      this.radioSelect = "";
    },
    getRowInfo(index) {
      this.radio = index;
      this.radioSelect = this.tableData[index];
    },
    // 取消弹框
    cancel() {
      this.radio = "";
      this.radioSelect = "";
      this.$emit("cancelImgShopDialog", false);
    },
    // 保存  向父组件传递图片url
    save() {
      if (!this.radioSelect) {
        this.$message.error("请选择一张图片");
        return false;
      }
      if (!this.radioSelect.imgUrl) {
        this.$message.error("请选择一张图片");
        return false;
      }
      this.$emit("saveImgUrl", this.radioSelect.imgUrl);
      this.$emit("cancelImgShopDialog", false);
    }
  }
};
</script>

<style lang="scss" scoped>
.herdertop {
  height: 8vh;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-sizing: border-box;
  .topImg {
    margin-right: 20px;
  }
}
.top1Bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 32px 10px 10px;
  font-size: 14px;
  border-bottom: 1px solid #ebeef5;
}
.bottomLeft {
  width: 100px;
}
.contentBox {
  margin: 20px;
  border: 10px solid #ebeef1;
  border-radius: 10px;
  .imgBox {
    .top1 {
      .top1Title {
        height: 56px;
        line-height: 56px;
        font-size: 20px;
        padding: 0 32px;
        font-weight: 700;
      }
    }
    .btns {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 32px 10px 32px;
      border-bottom: 1px solid #ebeef5;
    }
  }
}
</style>
<style lang="scss" scoped>
/deep/ .el-button--warning {
  background-color: rgb(255, 151, 72);
  border-color: rgb(255, 151, 72);
}
/deep/ .el-button {
  padding-top: 7px;
  padding-bottom: 7px;
}
.img {
  width: 70px;
  height: 40px;
}
.pagination {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 15px 15px 15px 0;
}
/deep/ .el-select {
  height: 30px;
  .el-input--suffix {
    height: 30px;
    .el-input__icon {
      line-height: 30px;
    }
    .el-input__inner {
      height: 30px;
    }
  }
}
/deep/ .el-dialog__body {
  padding-top: 10px;
  padding-bottom: 10px;
}
/deep/ .is-leaf {
  .el-checkbox {
    display: none;
  }
}
/deep/ .el-radio__label {
  display: none;
}
</style>
